/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.prep.view.command;

import java.awt.Point;
import java.awt.Toolkit;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.commons.view.DrawingArea;
import br.ufmg.dees.insane.ui.rich.prep.view.InterfacePrep;

/**
 * This class is an implementation of Command interface.
 * Its "execute" method gets the current point of DrawingArea
 * and sets its Shape as a line of a given initial point to
 * this new one.
 *
 * The initial point must be set wen this class is instantiated,
 * otherwise an NullPointerException will occur.
 *
 * It does exists to be set as a command of the DrawingArea to do
 * set the Shape at run time in order to create a preview when adding
 * a line to the model.
 *
 * <br>Example:<br>
 * <pre>
 * PreviewLineCommand pl = new PreviewLineCommand(parent);
 * pl.setInitialPoint(pt0);
 * drawingArea.setMovedCommand(pl);
 *</pre>
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since February 2004
 * @see    DrawingArea#getViewPoint
 * @see    DrawingArea#setShape
 * @see    DrawingArea#mouseMoved
 */
public class PreviewLineCommand implements Command {
    
    private InterfacePrep parent;
    private DrawingArea da;
    private Point ini, fin;
    private Point2D p2d;
    
//  ****************************************************************************
	
    /**
     * Creates new PreviewLineCommand and gets the
     * DrawingArea from parent.
     *
     * @param   parent The parent JInternalFrame
     * @see   InsaneInternalInterface#getDrawing
     */
    public PreviewLineCommand(JInternalFrame parent) {
        this.parent = (InterfacePrep)parent;
        this.da = this.parent.getView().getDrawingArea();
    }
    
//  ****************************************************************************
	
    /**
     * Gets the DrawingArea current view point and sets
     * its Shape as a line from the initial set point to this one.
     */
    public void execute() {
        try
        {
            fin = da.getViewPoint();
            da.setShape(new Line2D.Double(ini.x, ini.y, fin.x, fin.y));
        }
        catch (NullPointerException e)
        {
            Toolkit.getDefaultToolkit().beep();
            System.out.print(e.toString());
            JOptionPane.showMessageDialog(parent, e.toString(), "ERROR Message", JOptionPane.ERROR_MESSAGE);
        }
    }

//  ****************************************************************************
    
    public Point2D getFinalPoint()
    {
    	p2d = da.getWorldPoint(fin);
        return this.p2d;
    }    
    
//  ****************************************************************************
	
    /**
     * Sets the initial point to the preview line.
     *
     * @param po The desired point
     * @see java.awt.Point
     */
    public void setInitialPoint(Point po)
    {
        ini = po;
    }
    
//  ****************************************************************************
	
    /**
     * Impossible to undo - not implemented.
     */
    public void undo() {}
    
//  ****************************************************************************
	
    /**
     * Impossible to redo - not implemented.
     */
    public void redo() {}
    
//  ****************************************************************************
}

